home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.5)
-
- import os
- from xml.dom import minidom
- from xml.sax import saxutils
- from xml.parsers import expat
- from datetime import datetime
- from StringIO import StringIO
- import app
- import util
- import feed
- import views
- import prefs
- import config
- import folder
- import dialogs
- import eventloop
- from gtcache import gettext as _
- from gtcache import ngettext
-
- class Exporter(object):
-
- def __init__(self):
- self.io = StringIO()
- self.currentFolder = None
-
-
- def exportSubscriptions(self):
-
- callback = lambda p: self.exportSubscriptionsTo(p)
- title = _('Export OPML File')
- app.delegate.askForSavePathname(title, callback, None, u'miro_subscriptions.opml')
-
-
- def exportSubscriptionsTo(self, pathname):
- now = datetime.now()
- self.io.write(u'<?xml version="1.0" encoding="utf-8" ?>\n')
- self.io.write(u'<!-- OPML generated by Miro v%s on %s -->\n' % (config.get(prefs.APP_VERSION), now.ctime()))
- self.io.write(u'<opml version="2.0">\n')
- self.io.write(u'\t<head>\n')
- self.io.write(u'\t\t<title>%s</title>\n' % os.path.basename(pathname))
- self.io.write(u'\t\t<dateCreated>%s</dateCreated>\n' % now.ctime())
- self.io.write(u'\t\t<docs>http://www.opml.org/spec2</docs>\n')
- self.io.write(u'\t</head>\n')
- self.io.write(u'\t<body>\n')
- tabOrder = util.getSingletonDDBObject(views.channelTabOrder)
- for tab in tabOrder.getAllTabs():
- if tab.isChannelFolder():
- self._openFolderEntry(tab.obj)
- continue
- if tab.isFeed():
- self._writeFeedEntry(tab.obj)
- continue
-
- if self.currentFolder is not None:
- self._closeFolderEntry()
-
- self.io.write(u'\t</body>\n')
- self.io.write(u'</opml>\n')
- f = open(pathname, 'w')
- f.write(self.io.getvalue().encode('utf-8'))
- f.close()
-
- exportSubscriptionsTo = eventloop.asIdle(exportSubscriptionsTo)
-
- def _openFolderEntry(self, folder):
- if self.currentFolder is not None:
- self._closeFolderEntry()
-
- self.currentFolder = folder
- self.io.write(u'\t\t<outline text=%s>\n' % saxutils.quoteattr(folder.getTitle()))
-
-
- def _closeFolderEntry(self):
- self.io.write(u'\t\t</outline>\n')
-
-
- def _writeFeedEntry(self, thefeed):
- if self.currentFolder is not None and thefeed.getFolder() is None:
- self._closeFolderEntry()
- self.currentFolder = None
-
- if self.currentFolder is None:
- spacer = u'\t\t'
- else:
- spacer = u'\t\t\t'
- if isinstance(thefeed.getActualFeed(), feed.RSSFeedImpl):
- feedtype = u'type="rss"'
- else:
- feedtype = u'type="mirofeed"'
- self.io.write(u'%s<outline %s text=%s xmlUrl=%s />\n' % (spacer, feedtype, saxutils.quoteattr(thefeed.getTitle()), saxutils.quoteattr(thefeed.getURL())))
-
-
-
- class Importer(object):
-
- def __init__(self):
- self.currentFolder = None
- self.ignoredFeeds = 0
- self.importedFeeds = 0
-
-
- def importSubscriptions(self):
-
- callback = lambda p: self.importSubscriptionsFrom(p)
- title = _('Import OPML File')
- app.delegate.askForOpenPathname(title, callback, None, _('OPML Files'), [
- 'opml'])
-
-
- def importSubscriptionsFrom(self, pathname, showSummary = True):
- f = open(pathname, 'r')
- content = f.read()
- f.close()
-
- try:
- dom = minidom.parseString(content)
- root = dom.documentElement
- body = root.getElementsByTagName('body').pop()
- self._walkOutline(body)
- dom.unlink()
- if showSummary:
- self.showImportSummary()
- except expat.ExpatError:
- self.showXMLError()
-
-
- importSubscriptionsFrom = eventloop.asIdle(importSubscriptionsFrom)
-
- def showXMLError(self):
- title = _(u'OPML Import failed')
- message = _(u'The selected OPML file appears to be invalid. Import was interrupted.')
- dialog = dialogs.MessageBoxDialog(title, message)
- dialog.run()
-
-
- def showImportSummary(self):
- title = _(u'OPML Import summary')
- message = ngettext(u'Successfully imported %d feed.', u'Successfully imported %d feeds.', self.importedFeeds) % self.importedFeeds
- if self.ignoredFeeds > 0:
- message += '\n'
- message += ngettext(u'Skipped %d feed already present.', u'Skipped %d feeds already present.', self.ignoredFeeds) % self.ignoredFeeds
-
- dialog = dialogs.MessageBoxDialog(title, message)
- dialog.run()
-
-
- def _walkOutline(self, node):
-
- try:
- children = node.childNodes
- for child in children:
- if hasattr(child, 'getAttribute'):
- if child.hasAttribute('xmlUrl'):
- self._handleFeedEntry(child)
- else:
- self._handlerFolderEntry(child)
- child.hasAttribute('xmlUrl')
-
- self.currentFolder = None
- except Exception:
- e = None
- print e
-
-
-
- def _handleFeedEntry(self, entry):
- url = entry.getAttribute('xmlUrl')
- f = feed.getFeedByURL(url)
-
-
- def _handlerFolderEntry(self, entry):
- title = entry.getAttribute('text')
- self.currentFolder = folder.ChannelFolder(title)
- self._walkOutline(entry)
-
-
-